{include file="public/header"}
<body>
<div id="app">
    <div class="ht-main" v-cloak>
        <div class="ht-search">
            <el-form :inline="true">
                <el-form-item label="标题" size="small">
                    <el-input size="small" placeholder="标题" v-model="searchKey.title"></el-input>
                </el-form-item>

                <el-form-item size="small">
                    <el-button type="success" size="small" icon="el-icon-search" @click="search">搜索</el-button>
                    <el-button type="" size="small" icon="el-icon-refresh" @click="reset">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="ht-tool">
            <div class="ht-tool-left">
                <el-button type="success" size="mini" icon="el-icon-plus" @click="doAdd">添加</el-button>
                <el-button type="primary" :disabled="tableSelect.length !== 1" size="mini" icon="el-icon-edit" @click="edit(tableSelect[0])">编辑</el-button>
                <el-button type="danger" :disabled="!tableSelect.length" size="mini" icon="el-icon-delete" @click="batchDel('/admin/links/linkDel')">删除</el-button>
            </div>
        </div>
        <div class="ht-main-body">
            <el-table :data="tableData" border header-row-class-name="ht-table-header" @selection-change="handleSelectChange">
                <el-table-column
                        align="center"
                        type="selection"
                        width="39">
                </el-table-column>
                <el-table-column
                        align="center"
                        prop="id"
                        label="ID"
                        width="90">
                </el-table-column>
                <el-table-column
                        align="center"
                        prop="title"
                        label="标题">
                </el-table-column>
                <el-table-column
                        align="center"
                        prop="url"
                        label="链接">
                </el-table-column>
                <el-table-column
                        align="center"
                        prop="bz"
                        label="备注"
                        width="100">
                </el-table-column>
                <el-table-column
                        align="center"
                        prop="status"
                        label="状态"
                        width="80">
                    <template slot-scope="scope">
                        <el-switch v-model="scope.row.status"
                                   active-color="#13ce66"
                                   inactive-color="#eeeeee"
                                   @change="changeStatus($event,scope.row.id)"
                                   :active-value="1"
                                   :inactive-value="0">
                        </el-switch>
                    </template>
                </el-table-column>
                <el-table-column align="center"
                                 prop="create_time"
                                 label="发布时间"
                                 width="180">
                </el-table-column>
                <el-table-column
                        align="center"
                        prop="sort"
                        label="排序"
                        width="80">
                </el-table-column>
                <el-table-column
                        align="center"
                        label="操作"
                        width="150">
                    <template slot-scope="scope">
                        <el-button @click="edit(scope.row)" size="small" type="primary" icon="el-icon-edit"></el-button>
                        <el-button @click="del(scope.row)" size="mini" type="danger" icon="el-icon-delete"></el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="ht-main-page">
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="page"
                        :page-sizes="[1,10, 200, 300, 400]"
                        :page-size="limit"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </div>
            <el-drawer
                    :visible.sync="is_edit"
                    size="100%"
                    :with-header="false"
                    custom-class="ht-drawer-bg"
            >
                <div class="ht-drawer-main">
                    <el-page-header @back="goBack" title="Esc返回" :content="content"></el-page-header>
                    <div class="ht-form">
                        <el-form :model="editData" label-width="90px">
                            <el-form-item label="标题" label-width="80px">
                                <el-input placeholder="标题" v-model="editData.title"
                                          style="width: 50%"></el-input>
                            </el-form-item>
                            <el-form-item label="链接" label-width="80px">
                                <el-input placeholder="http开头" v-model="editData.url"
                                          style="width: 50%"></el-input>
                            </el-form-item>
                            <el-form-item label="排序" label-width="80px">
                                <el-input placeholder="排序" v-model="editData.sort"
                                          style="width: 50%"></el-input>
                            </el-form-item>
                            <el-form-item label="备注" label-width="80px">
                                <el-input placeholder="备注" v-model="editData.bz" type="textarea"
                                          style="width: 50%"></el-input>
                            </el-form-item>

                            <el-form-item label-width="80px">
                                <el-button @click="goBack">取消</el-button>
                                <el-button type="primary" @click="onSubmit">提交</el-button>
                            </el-form-item>
                        </el-form>
                    </div>
                </div>
            </el-drawer>

        </div>

    </div>
</div>


{include file="public/common"}
<script>
    new Vue({
        el: "#app",
        data: function () {
            return {
                arctypeList: [],
                activeTab: 'first',
                tableData: [],
                is_edit: false,
                editData: {
                    "status": 1,
                    "title": '',
                    "bz": "",
                    "url": "",
                    "sort":0,
                },
                editDataTemp: {
                    "status": 1,
                    "title": '',
                    "bz": "",
                    "url": "",
                    "sort":0,
                },
                content: '编辑',
                total: 0,
                page: 1,
                limit: 10,
                search_arctype_id: "",
                search_title: "",
                editBody: '13123',
                pre_view: []
            }
        },
        created: function () {
            this.getData()
        },
        methods: {
            uploadSuccess(response, file, fileList) {
                if (response.code === 1) {
                    this.editDataTemp.litpic = response.url
                    this.editData.litpic = response.url
                    this.pre_view = [response.url]
                    console.log('response.url', this.editData.litpic)
                }
                console.log(response, file, fileList)
            },
            changeStatus: function (e, id) {
                axios.post("{:url('Links/linkStatus')}", {id: id}).then((res) => {
                    if (res.data.code == 1) {
                        this.$message({
                            message: '操作成功',
                            type: 'success',
                            duration: 1000,
                        })
                    } else {
                        this.$message({
                            message: res.data.msg,
                            type: 'error',
                            duration: 1500,
                            onClose: function () {
                                location.reload()
                            }
                        })
                    }
                })
            },
            doSearch: function () {
                this.getData()
            },
            onSubmit() {
                var that = this
                $.post('{:url("Links/linkAdd")}', that.editData, function (res) {
                    if (res.code == 1) {
                        that.showSuccessMsgCall(function () {
                            location.reload();
                        })
                    } else {
                        that.showErrorMsg(res.msg)
                    }
                })

            },
            getData: function () {
                var that = this
                axios.post('{:url("Links/linkList")}', {
                    limit: that.limit,
                    page: that.page,
                }).then((res) => {
                    console.log(res.data)
                    that.tableData = res.data.data.data
                    that.total = res.data.data.total
                    that.pre_view = that.tableData.map(function (item,index,arr){
                        return item.litpic
                    })
                })
            },
            doAdd: function () {
                this.is_edit = true
                this.content = '添加'
                this.editData = this.editDataTemp
            },
            edit: function (data) {
                this.is_edit = true
                this.content = '编辑'
                this.editData = data
                console.log(data)
            },
            goBack: function () {
                this.is_edit = false
            },
            del: function (data) {
                var that = this
                that.$confirm('确定删除么?', '提示', {
                    type: 'warning',
                    lockScroll: false
                }).then(() => {
                    axios.post('{:url("Links/linkDel")}', {id: data.id}).then((res) => {
                        let result = res.data;
                        if (result.code === 1) {
                            that.showSuccessMsgCall(function () {
                                location.reload()
                            })
                        } else {
                            that.showErrorMsg(result.msg)
                        }
                    })
                })
            },
            handleSizeChange: function (e) {
                this.limit = e
            },
            handleCurrentChange: function (e) {
                this.page = e
            }
        },
        watch: {
            page(v) {
                this.getData()
            },
            limit() {
                this.getData()
            }
        }
    })
</script>
</body>
</html>
